type UserPermissions = "read" | "write" | "delete";

type PermissionFlags = {
  [K in UserPermissions as `can${Capitalize<K & string>}`]: boolean;
};
const user: PermissionFlags = {
  canRead: true,
  canWrite: true,
  canDelete: true,
};
// ================ is使用
type Product = { type: "product"; price: number };
type User = { type: "user"; email: string };
type Result = Product | User;

function isProduct(r: Result): r is Product {
  return r.type === "product";
}

const results: Result[] = [
  { type: "product", price: 100 },
  { type: "user", email: "test@example.com" },
  { type: "product", price: 200 },
];

const prices = results.filter(isProduct).map((p) => p.price);

const toUpper = (s: string) => s.toUpperCase();
const getLength = (s: string) => s.length;
type Pipe<T, R> = (input: T) => R; // 类型变量的定义
const pipe: Pipe<string, number> = (str) => getLength(toUpper(str));

function parseData(type: "json"): object;
function parseData(type: "text"): string;
function parseData(type: string): any {
  // 实际解析逻辑
}
const jsonResult = parseData("json"); // TypeScript 会自动推断这是 object 类型

// infer A 是关键：它会 推断 函数参数的类型并将其赋值给类型变量 A
type Params<T> = T extends (...args: infer A) => any ? A : never;
type MyFn = (x: number, y: string) => boolean;
type Args = Params<MyFn>; // [number, string]，完美提取！
function add(a: number, b: string, c: boolean): void {}
// 使用 Params 提取参数类型
type AddParams = Params<typeof add>;
